#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <set>

using namespace std;


class cirQue
{
public:
    cirQue(int n): size(n+1), read(0), write(0), buffer(n+1, 0){}
    bool isEmpty() {return read == write;}
    bool isFull() {
	bool ans = (write+1)%size == read;
	return ans;
    }

    bool enque(int num) {
        dump();
        //cout <<"Read: " << read << " " << "Write: " << write << " ";
        if (isFull()) return false;
        buffer[write++] = num;
        write %= size;
        return true;
    }

    int deque() {
        dump();
        //cout <<"Read: " << read << " " << "Write: " << write << " ";
	if (isEmpty()) return -1;
	int rtn = buffer[read++];
	read %= size;
        return rtn;
    }
    
    void dump()
    {
	for (int i = 0; i < buffer.size(); i++) 
	    cout << buffer[i] << " ";
	cout << endl;
    }
private:
    int size;
    int read, write;
    vector<int> buffer;
};

int main(int argc, char **argv)
{
   int a[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18}; 
   cirQue myQue(9);;
   for (int i = 0; i < sizeof(a)/sizeof(int); i++) {
    int choice = rand()%2;
    if (choice == 0) {
	int num = myQue.deque();
        i--;
        cout << "Deque: " << num << endl;	
    } else {
	myQue.enque(a[i]);
        cout << "Enque: " << a[i] << endl;	
    }
   } 
   return 0;
}
